@var isNew = sysUser.ID! == null;
@var employee_number = 'required;';
@var account = 'required;';
@if(isNew){
@employee_number = employee_number + 'remote[' + basePath + '/sys/user/verifyEmployeeNumber, sysUser.ID];';
@account = account + 'remote[' + basePath + '/sys/user/verifyAccount, sysUser.ID];';
@}
<link rel="stylesheet" href="${baseStaticUrl}/plugins/ztree/css/zTreeStyle/zTreeStyle.css" type="text/css">
<script type="text/javascript" src="${baseStaticUrl}/plugins/ztree/js/jquery.ztree.core.js"></script>
<script type="text/javascript" src="${baseStaticUrl}/plugins/ztree/js/jquery.ztree.excheck.js"></script>
<div class="form-container">
	<div class="box">
        <form action="#" method="post" name="suvForm" id="suvForm">
        	<div class="row visa-row">
                <div id="wait-msg" class="alert alert-info"
                     style="background:url(${baseStaticUrl}/common/img/ajax-loader.gif) no-repeat scroll 0.5em 50%;display:none"></div>
                <div id="alert-msg" class="alert alert-success" style="display:none"></div>
                <div id="error-msg" class="alert alert-danger" style="display:none"></div>
            </div>
            <div class="row visa-row">
            	<h6 class="mg-b-20 h6-mar-sm">
                    <strong>用户管理</strong>
                </h6>
                <div class="table-responsive">
                   <div class="row w-row">
                   		<div class="col-md-1 col-xs-12 w-row1"><span class="span_red">*</span>员工编号
                        </div>
                        <div class="col-md-3 col-xs-12 w-row2">
                                <input type="text" class="form-control form-control-sm" id="ygbh"
                                       name="sysUser.EMPLOYEE_NUMBER" ${isNew ? '' : ' readonly="readonly" '}
                                value="${sysUser.EMPLOYEE_NUMBER}" maxlength="75"
                                data-rule="员工编号:${employee_number}">
                        </div>
                        <div class="col-md-1 col-xs-12 w-row1"><span class="span_red">*</span>用户名
                         </div>
                        <div class="col-md-3 col-xs-12 w-row2">
                            <input type="text" class="form-control form-control-sm"
                                   ${isNew ? '' : 'readonly="readonly" '}
                            id="yhm" maxlength="75"
                            data-rule="用户名:${account}"
                            name="sysUser.ACCOUNT" value="${sysUser.ACCOUNT}">
                            <input type="hidden" name="sysUser.ID" value="${sysUser.ID!}" id="ID">
                         </div>
                         <div class="col-md-1 col-xs-12 w-row1"><span class="span_red">*</span>姓名
                         </div>
                         <div class="col-md-3 col-xs-12 w-row2">
                             <input type="text" id="userName" class="form-control form-control-sm"
                                    name="sysUser.USER_NAME"
                                    value="${sysUser.USER_NAME}" maxlength="75"
                                    data-rule="姓名:required;">
                         </div>
                   </div>
                   <div class="row w-row">
                        <div class="col-md-1 col-xs-12 w-row1">部门</div>
                        <div class="col-md-3 col-xs-12 w-row2">
                            <input type="hidden" id="dept_value" name="sysUser.DEPT_ID" value="${sysUser.DEPT_ID!}">
                         	<div class="tdself">
	                             <input type="text" class="form-control form-control-sm" id="dept_show"
	                                    readonly="readonly"
	                                    value="${sysUser.DEPT_SHOW!}">
		                          <span class="btn visa-btn-icon visa-btn-select fa fa-search fa-1" data-toggle="modal"
		                                data-target="#selectDeptModal"
		                                aria-hidden="true"></span>
                          	  </div>
                         </div>
                         <div class="col-md-1 col-xs-12 w-row1">职级</div>
                         <div class="col-md-3 col-xs-12 w-row2">
                             <input id="zj" type="text" class="form-control form-control-sm"
                                    name="sysUser.POSITION_LEVEL" maxlength="60"
                                    value="${sysUser.POSITION_LEVEL!}"/>
                         </div>
                          <div class="col-md-1 col-xs-12 w-row1">职位</div>
                         <div class="col-md-3 col-xs-12 w-row2">
                         <input type="text" class="form-control form-control-sm" id="title" name="sysUser.TITLE"
                                    value="${sysUser.TITLE}" maxlength="150"> </div>
                   </div>
                   <div class="row w-row">
                          <div class="col-md-1 col-xs-12 w-row1">邮箱</div>
                          <div class="col-md-3 col-xs-12 w-row2"><input type="text" class="form-control form-control-sm" id="yx" name="sysUser.EMAIL"
                                     value="${sysUser.EMAIL}" maxlength="150"
                                     data-rule="邮箱:email;"> </div>
                          <div class="col-md-1 col-xs-12 w-row1">联系方式</div>
                          <div class="col-md-3 col-xs-12 w-row2"><input type="text" class="form-control form-control-sm" id="lxfs"
                                     name="sysUser.PHONE_NUMBER" data-rule="rulePhoneNum;"
                                     value="${sysUser.PHONE_NUMBER}" maxlength="150"> </div>
                          <div class="col-md-1 col-xs-12 w-row1"><span class="span_red">*</span>员工状态</div>
	                   	  <div class="col-md-3 col-xs-12 w-row2">
	                             <epoch:radio name="sysUser.STATUS" id="STATUS"
	                                          list="${lisFun.getDictCode('SYS_USER_STATUS')}"
	                                          checked="${sysUser.ID!=null ? sysUser.STATUS:'ENABLE'}" classValue="radio-inline"></epoch:radio>
	                      </div>
                   </div>
                   @if(sysUser.ID! != null){
                   <div class="row w-row" style="margin-right: 20px;">
                        <div class="col-md-12 col-xs-12 w-row1">
                            <button type="button" class="btn btn-primary btn-sm" onclick="resetPassword()">重置密码</button>
                        </div>
                    </div>
                    @}
                </div>
            </div>
        </form>
    </div>
    <div class="box">
    	<div class="row w-row">	
			<epoch:table id="userSysRole"
                        queryUrl="${basePath}/sys/user/findUserRole?roleType=SYSTEM&userId=${sysUser.ID!}" rowId="id"
                        tableName="系统角色分配"
                        delRowBtn="true" editable="true">
                   <epoch:moreBtn position="0">
	                    <button type="button" class="btn btn-primary btn-sm" onclick="allocateSysRole();">
	                        <span class="glyphicon glyphicon-plus-sign" aria-hidden="true"></span>分配角色
	                    </button>
			       </epoch:moreBtn>
	               <epoch:tr check="checkbox" titleTip="全选"></epoch:tr>
	               <epoch:tr name="角色名称" formName="role_name"
	                      editorType="false"></epoch:tr>
           </epoch:table>
    	</div>
    </div>
</div>

<!--角色选择弹框-->
<epoch:winOpen id="assignRolePopup" title="角色选择弹框" backdrop="true"
            width="680px" height="450px">
    <epoch:table id="assignRoleTable" queryUrl="${basePath}/sys/role/findRoleForUser?roleType=SYSTEM" rowId="id" showRefresh="true">
           <epoch:tr check="checkbox" titleTip="全选"></epoch:tr>
           <epoch:tr name="角色名称" formName="role_name">
           	</epoch:tr>
           <epoch:tr name="角色编码" formName="role_code" width="150"> </epoch:tr>
           <epoch:tr name="描述" formName="comments" width="150"> </epoch:tr>
     </epoch:table>
</epoch:winOpen>

<!--部门选择-->
<epoch:winOpen id="selectDeptModal" title="分配部门" backdrop="true"
            width="380px" height="450px">
    <div style="width:100%;height:100%;overflow-y:scroll;">
        <ul id="sysZrTreeBean" class="ztree"></ul>
    </div>
</epoch:winOpen>

<script type="text/javascript">
	
	_headToolBar.saveBtn(null, null, suv);
	
	//菜单组织树
	var zTreeObj;
	var setting = {
	    check: {chkStyle: "radio", enable: true, radioType: "all"},
	    data: {simpleData: {enable: true}},
	    callback: {onCheck: zTreeOnClick}
	};
	var zNodes = [{
	    name: "组织结构树",
	    id: "0",
	    open: true,
	    children:${treeNodeJson}
	}];
	$(document).ready(function () {
	    zTreeObj = $.fn.zTree.init($("#sysZrTreeBean"), setting, zNodes);
	});
	var ids = "";
	var idsName = "";
	function zTreeOnClick(event, treeId, treeNode) {
	    ids = treeNode.id;
	    idsName = treeNode.name;
	}
	
	//获取所有选中的组织
    $("#selectDeptModal_save").click(function () {
        if (isEmpty(ids)) {
        	MessageBox.error("请选择部门！");
            return false;
        }
        $("#dept_show").val(idsName);
        $("#dept_value").val(ids);
        $("#selectDeptModal").modal('hide');
    });
	
  	//获取所有选中的组织
    $("#assignRolePopup_save").click(function () {
        var datas = $("#assignRoleTable").bootstrapTable('getSelections');
        var nowData = $("#userSysRole").bootstrapTable('getData');
        var newRows=[];
        for (var i = 0; i < datas.length; i++) {
        	var data={};
        	data.id = getUUID();
        	data.user_id = "${sysUser.ID!}",
        	data.role_id = datas[i].id,
        	data.role_name = datas[i].role_name;
        	newRows.push(data);
		}
        $("#userSysRole").bootstrapTable("cer_appendRows", newRows, true, true);
        $("#assignRolePopup").modal('hide');
    });
    
    $("#suvForm").validator({
        focusInvalid: false,//失败自动获得焦点
        focusCleanup: true,//获得焦点自动清除
        stopOnError: false,//错误既停止开关
        msgMaker: false,//不要自动生成消息
        timely: false,
        rules: {
            rulePhoneNum: [/^[-+#*0-9]{3,}$/, "请填写有效的联系方式！"]
        },
        valid: function (form) {
            saveFun();
        }
    }).on("validation", function (e, current) {
        if (!current.isValid) {
            appendTipMsgRow("error-msg", current.msg);
        }
    });
    
    function saveFun() {
        var formData = formToObj("suvForm");
        formData.userSysRoles = $("#userSysRole").bootstrapTable("cer_confirmEditAllRows", true);
        $.ajax({
            url: "${basePath}/sys/user/saveOrUpdate",
            data: formData,
            type: "post",
            success: function (result) {
                if (result) {
                    if (result.code == "0") {
                        MessageBox.success("保存成功。");
                        //跳转到选项卡1
                        _tabs.showPage("list", null, null, null);
                    } else {
                        MessageBox.error("保存失败！");
                    }
                } else {
                    MessageBox.error("保存失败！");
                }
            },
            error: function () {
                MessageBox.error("服务器错误，请稍后再试！");
            },
            async: false
        });
    }
  	
    //保存、更新
    function suv() {
        $("#suvForm").trigger("validate");
    }
	
    function allocateSysRole() {
    	$("#assignRolePopup").modal('show');
    }
    
    function formToObj(formId) {
        var data = null;
        var formArr = $("#" + formId).serializeArray();
        if (formArr != null) {
            data = {};
            $(formArr).each(function (i, elmt) {
                if (data[elmt.name]) {
                    if ($.isArray(data[elmt.name])) {
                        data[elmt.name].push(elmt.value);
                    } else {
                        data[elmt.name] = [data[elmt.name], elmt.value];
                    }
                } else {
                    data[elmt.name] = elmt.value;
                }
            });
        }
        return data;
    }
    
    function resetPassword() {
        if (MessageBox.confirm("确定要重置密码吗？")) {
            var id = $("#ID").val();
            $.post("${ctxPath}/sys/user/resetPassword", {"ID": id}, function (msg) {
                if (msg == "true") {
                    MessageBox.error("重置密码成功。");
                } else {
                    MessageBox.error("置密码失败！");
                }
            });
        }
    }
</script>